import Vue from 'vue'
import Router from 'vue-router'
import request from '../script/request'

Vue.use(Router)

const router = new Router({
	mode:'history',
	routes: [
		{
			path:'/',
			name: 'home',
            component: () =>
                import('../views/home.vue')
		},
		// {
		// 	path: '/',
		// 	name: 'zhuzhuang',
		// 	meta: {
		// 		title: '',
		// 		keepAlive: true,
		// 	},
		// 	component: () =>
		// 		import('../views/index.vue')
		// },
		// {
		// 	path: '/zhexian',
		// 	name: 'zhexian',
		// 	meta: {
		// 		title: '',
		// 		keepAlive: true,
		// 	},
		// 	component: () =>
		// 		import('../views/zhexian.vue')
		// },
		// {
		// 	path: '/bing',
		// 	name: 'bing',
		// 	meta: {
		// 		title: '',
		// 		keepAlive: true,
		// 	},
		// 	component: () =>
		// 		import('../views/bing.vue')
		// },
		// {
		// 	path: '/leida',
		// 	name: 'leida',
		// 	meta: {
		// 		title: '',
		// 		keepAlive: true,
		// 	},
		// 	component: () =>
		// 		import('../views/leida.vue')
		// },
	]
})

function filterAsyncRouter(asyncRouterMap) { //遍历后台传来的路由字符串，转换为组件对象
    console.log(asyncRouterMap,8888)
    const accessedRouters = asyncRouterMap.filter(route => {
      if (route.component) {
    if (route.component === 'Layout') {//Layout组件特殊处理
          route.component = Layout
        } else {
          route.component = _import(route.component)
        }
      }
      if (route.children && route.children.length) {
        route.children = filterAsyncRouter(route.children)
      }
      return true
    })
  
    return accessedRouters
  }


var getRouter //用来获取后台拿到的路由

router.beforeEach((to, from, next) => {
  if (!getRouter) {//不加这个判断，路由会陷入死循环
    console.log(getObjArr('router') == null)
    //if (typeof getObjArr('router') == null) {
      request.get('/menu',function(res) {
          console.log(res)
        getRouter = res.data.data.router//后台拿到路由
        saveObjArr('router', getRouter) //存储路由到localStorage

        routerGo(to, next)//执行路由跳转方法
      },fail => {

      })
    // } else {//从localStorage拿到了路由
    //   getRouter = getObjArr('router')//拿到路由
    //   routerGo(to, next)
    // }
  } else {
    next()
  }

})


function routerGo(to, next) {
  getRouter = filterAsyncRouter(getRouter) //过滤路由
  console.log(getRouter,11)
  router.addRoutes(getRouter) //动态添加路由
  global.antRouter = getRouter //将路由数据传递给全局变量，做侧边栏菜单渲染工作
  next({ ...to, replace: true })
}

function saveObjArr(name, data) { //localStorage 存储数组对象的方法
  localStorage.setItem(name, JSON.stringify(data))
}

function getObjArr(name) { //localStorage 获取数组对象的方法
  return JSON.parse(window.localStorage.getItem(name));

}


export default router;